package com.aios.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.ControllerMapper;
import com.aios.dao.SystemUserHasControllerMapper;
import com.aios.entity.Controller;
import com.aios.entity.Switch;
import com.aios.entity.SwitchPanel;
import com.aios.entity.SystemUserHasController;
import com.aios.entity.unit.StatusEnum;
import com.aios.service.ControllerService;
import com.aios.service.ExistValidationEnum;
import com.aios.util.AiosAccessUtil;
import com.aios.util.GuidGenerator;
import com.aios.util.HttpClientExecuter;
import com.aios.util.HttpClientAdaptor.HttpClientResponse;

@Service("controllerService")
public class ControllerServiceImpl extends AbstractService implements
		ControllerService {

	@Autowired
	private ControllerMapper controllerMapper;

	@Autowired
	private SystemUserHasControllerMapper systemUserHasControllerMapper;

	@Override
	public List<Controller> findAll() {
		return controllerMapper.findAllByStatus(StatusEnum.ACTIVE
				.getStatusValue());
	}

	@Override
	public List<Controller> findAllNotDisabled() {
		return findAll();
	}

	@Override
	public List<Controller> findAllDisabled() {
		return controllerMapper.findAllByStatus(1);
	}

	@Override
	public Controller findById(String id) {
		return controllerMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(Controller entity) {
		return this.controllerMapper.insert(entity);
	}

	@Override
	public int update(Controller entity) {
		return this.controllerMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(Controller entity) {
		return this.delete(entity.getId());
	}

	@Override
	public int delete(String id) {
		return controllerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Controller> findControllerByUserID(String id) {
		return controllerMapper.findControllerByUserID(id);
	}

	@Override
	public ExistValidationEnum nameExistValidation(String name) {
		List<Controller> list = controllerMapper.findByName(name);

		if (list == null || list.size() == 0) {
			return ExistValidationEnum.NOT_EXIST;
		} else if (list.size() == 1) {
			return ExistValidationEnum.EXIST;
		} else {
			return ExistValidationEnum.MULTIPLE_EXIST;
		}
	}

	@Override
	public ExistValidationEnum nameExistValidation(String id, String name, String userId) {

		List<Controller> list = controllerMapper.findByNameNotWithId(id, name, userId);

		if (list == null || list.size() == 0) {
			return ExistValidationEnum.NOT_EXIST;
		} else if (list.size() == 1) {
			return ExistValidationEnum.EXIST;
		} else {
			return ExistValidationEnum.MULTIPLE_EXIST;
		}
	}

	@Override
	public int save(Controller entity, String userId) {
		int returnVal = 0;

		saveAddSettings(entity, userId);
		entity.setId(GuidGenerator.controllerKey());
		returnVal = save(entity);

		SystemUserHasController record = new SystemUserHasController();
		record.setControllerId(entity.getId());
		record.setSystemUserId(userId);
		//record.setId(GuidGenerator.systemUserHasControllerKey());
		saveAddSettings(record, userId);

		returnVal = systemUserHasControllerMapper.insert(record);

		return returnVal;
	}

	@Override
	public int update(Controller entity, String userId) {
		int returnVal = 0;
		
		saveEditSettings(entity, userId);
		returnVal = controllerMapper.updateByPrimaryKeySelective(entity);
		
		return returnVal;
	}

	@Override
	public List<Controller> findByUserId(String userId) {
		return controllerMapper.findByUserId(userId);
	}
	
	public boolean changeCameraDirection(String controllerId, int direction) {

		//Switch switchButton = switchMapper.selectByPrimaryKey(switchId);
		//SwitchPanel switchPanel = switchPanelMapper.findById(panelId);
		Controller controller=controllerMapper.selectByPrimaryKey(controllerId);

		if (controller == null ) {
			return true;
		}

		HttpClientResponse response = HttpClientExecuter
				.getCameraChangeDirection(controller.getProtocol(), controller.getUrl(),
						controller.getHttpPort(), controller.getUserName(),
						controller.getUserPassword(),
						AiosAccessUtil.REVOLVE_CAMERA_URI,AiosAccessUtil.ROTATE_DIRECTION[direction] , true);

		if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
			return true;
		}

		return false;
	}
	
	public boolean changeCameraZoom(String controllerId,String mode) {

		//Switch switchButton = switchMapper.selectByPrimaryKey(switchId);
		//SwitchPanel switchPanel = switchPanelMapper.findById(panelId);
		Controller controller=controllerMapper.selectByPrimaryKey(controllerId);

		if (controller == null ) {
			return true;
		}

		HttpClientResponse response = HttpClientExecuter
				.getCameraChangeZoom(controller.getProtocol(), controller.getUrl(),
						controller.getHttpPort(), controller.getUserName(),
						controller.getUserPassword(),
						AiosAccessUtil.ZOOM_CAMERA_URI,mode, true);

		if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
			return true;
		}

		return false;
	}

}
